package com.g24.partychecker;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Favorieten extends ListActivity 
{	
	public DBAdapter mDbHelper;
	private int MENU_DELETE_ALL;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_list);
        mDbHelper = new DBAdapter(this);
        mDbHelper.open();
        fillData();
    }
    
    private void fillData() 
    {
        // Get all of the notes from the database and create the item list
        Cursor c = mDbHelper.fetchAllEvents();
        startManagingCursor(c);

        String[] from = new String[] { DBAdapter.KEY_NAAM };
        int[] to = new int[] { R.id.events_row_text };
        
        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.events_row, c, from, to);
        setListAdapter(notes);
        
        final ListView lv2 = getListView();
	  	lv2.setTextFilterEnabled(true);

	  	lv2.setOnItemClickListener(new OnItemClickListener() 
	  	{
	  		public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
	  		{
	  		    long longID = lv2.getItemIdAtPosition(position);
	  		    
	  		    System.out.println(longID);

	  			Intent i = new  Intent();
	    		i.putExtra("FEESTID", longID);
	    		i.setClassName("com.g24.partychecker", "com.g24.partychecker.Favinfo");
	            startActivity(i);
	  		}
	  	});
	  	
	  	//mDbHelper.close();
	  	
    }
    
    /* Creates the menu items */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, MENU_DELETE_ALL, 0, "Alles Verwijderen").setIcon(R.drawable.ic_menu_close);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if(item.getItemId() == MENU_DELETE_ALL) 
        {
    		deleteAll();
			return true;
        }
        return false;
    }

	private void deleteAll() 
	{
		mDbHelper.deleteAllEvents();
		
		Context context = getApplicationContext();
 	   	CharSequence text = "Alles verwijderd";
 	   	int duration = Toast.LENGTH_SHORT;
 	   	Toast toast = Toast.makeText(context, text, duration);
 	   	toast.show();
 	   	Toast.makeText(context, text, duration).show();
		
		Intent z = new Intent(Favorieten.this, Hoofdscherm.class);
        startActivity(z);
	}
}
